#include "Common.h"

using namespace std;

int PlayersTurn(DIFFICULTY myDifficulty)
{
	int guess, tries = 0;
	int theNumber = rand() % myDifficulty + 1; // random number between 1 and 10

	cout << "===================" << endl;
	cout << "===PLAYERS' TURN===" << endl;
	cout << "===================" << endl;

	// First try for the player.
	cout << "Guess a number between 1 and "<< myDifficulty << ": ";
	cin >> guess;
	++tries;

	/////////////////
	// PLAYER LOOP //
	/////////////////
	while (guess != theNumber)
	{
		if (guess > (theNumber * 2))
			cout << "Way too high! ";
		else if (guess > theNumber)
			cout << "Too high! ";

		if (guess < (theNumber / 2))
			cout << "Way too low! ";
		else if (guess < theNumber)
			cout << "Too low! ";

		// Second try or further tries for the player.
		cout << "Try again: ";
		cin >> guess;
		++tries;
	}

	cout << "\nWell done! You guessed that the number was " << theNumber << ".\n";
	if (tries == 1)
		cout << "You got it first try! AMAZING! w00t!\n\n";
	else if (tries < 5)
		cout << "It only took you " << tries << " tries, nice one!\n\n";
	else if (tries < 10)
		cout << "You still did pretty well! Guessing the number in " << tries << " tries.\n\n";
	else
		cout << "Well... You got there in the end. " << tries << " tries...\n\n";

	// return the number of tries for score compare
	return tries;
}

int ComputersTurn(DIFFICULTY myDifficulty)
{
	int guess, tries = 0;
	int theNumber;
	int lowest = 1, highest = myDifficulty;

	cout << "===================" << endl;
	cout << "==COMPUTER'S TURN==" << endl;
	cout << "===================" << endl << endl;

	while (true)
	{
		cout << "Enter a number between 1 and "<< myDifficulty << ": ";
		cin >> int(theNumber);

		if (!(theNumber >= 1 && theNumber <= myDifficulty))
			cout << "Incorrect value. ";
		else
			break;
	}

	///////////////////
	// COMPUTER LOOP //
	///////////////////
	do
	{

		guess = rand() % ((highest - lowest) + 1) + lowest; // (Highest - Lowest + 1) is the range.
		++tries;

		cout << "The computer's guesses that your number is " << guess << " !\n";

		if (guess > theNumber)
		{
			highest = guess - 1; // too high = guess lower
		}

		if (guess < theNumber)
		{
			lowest = guess + 1; // too low = guess higher
		}

	} while (guess != theNumber);

	cout << "\nThe computer guessed your number after " << tries << " tries.\n\n";

	// return the number of tries for score compare
	return tries;
}